package com.task.scheduling.processor.contract_processor;

import com.task.scheduling.enums.TaskStatus;
import com.task.scheduling.model.TaskResult;
import com.task.scheduling.model.TaskSchedulingModel;
import com.task.scheduling.processor.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @program: task-scheduling
 * @description: 更新tair缓存
 * @author: zhouliangliang
 * @create: 2018-11-20 19:37
 **/
@Component
public class UpdateTairCacheProcessor implements Processor {

    private Logger log = LoggerFactory.getLogger(UpdateTairCacheProcessor.class);

    @Override
    public TaskResult process(TaskSchedulingModel model) throws Exception{
        log.info("任务：{} 开始执行", model.getId());
        boolean execFlag = true;
        int num = 0;
        while(execFlag == true) {
            if(!Thread.currentThread().isInterrupted()) {
                num++;
                this.idle();
                log.info("Thread.currentThread().getId() : {} , taskid : {} ", Thread.currentThread().getId(), model.getId());
                if(num > 200) {
                    break;
                } else {
                    continue;
                }
            } else {
                log.info("任务{}执行被中断，任务退出!", model.getId());
                return new TaskResult(TaskStatus.INTERRUPTED.getCode(), "任务: " + model.getId() + "被中断执行");
            }
        }
        return new TaskResult(TaskStatus.SUCCESS.getCode(), "任务: " + model.getId() + "执行完毕");
    }

    public void idle() {
        int num = 0;
        for(long i=0; i<5000l; i++) {
            for(int j=0; j<1000000; j++) {
                num = num + j;
            }
            System.out.println(num);
            num = 0;
        }
    }
}
